var path = require('path');
import express from 'express'
import bodyParser from 'body-parser'
var app = express();
import config from './config/config/index'
import cookie from 'cookie-parser'
import session from 'express-session'
import router from './routes/index'
import dtime from 'time-formater'
console.log(dtime().format('YYYY-MM-DD HH:mm:ss'))

app.all('*', (req, res, next) => {
    const {
        origin,
        Origin,
        referer,
        Referer
    } = req.headers
    const allowOrigin = origin || Origin || referer || Referer || '*'
    res.header('Access-Control-Allow-Origin', allowOrigin)
    res.header('Access-Control-Allow-Headers', 'Content-Type,Authorization,x-Requested-with')
    res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
    res.header('Access-Control-Allow-Credentials', true) // 可以带cookies
    res.header('X-Powered-By', 'Express')
    if (req.method === 'OPTIONS') {
        res.sendStatus(200)
    } else {
        next()
    }
})
app.use(cookie());
app.use(session({
    secret: 'this is string key',
    name:'session_id',
    resave: false
}))
app.use(bodyParser.json());
app.use(session({
    secret: 'keyboard cat',
    resave: false,
    saveUninitialized: true,
    cookie: { secure: true }
}));
app.use("/img",express.static(path.join(__dirname, 'img')));
app.use(bodyParser.urlencoded({ extended: false }));
router(app);
app.listen(config.port,()=>{
    console.log(`start${config.port}`)
})
module.exports = app;
